import {scanResize} from "@/pages/wordPage/src/objs/scanResize";
import screen from "@/pages/wordPage/src/screen";
import utils from "@/pages/wordPage/src/utils";
import word from "@/pages/wordPage/src/objs/word";
import p5 from 'p5';
import {ElMessage} from "element-plus";
import message from "@/pages/wordPage/src/objs/message";
import setting from "@/setting";
import {getMyWord} from "@/apis/apis";

let c ;
let element;
let p5s;
let maxFps=420;
let offscreenCanvas1,offscreenCanvas2;
export default {

    init:async function (){
        this.clearAllWord();
        element = document.getElementById("wordCanvas");
        c=element.getContext("2d");
        this.setCancansFullScreen();
        window.addEventListener('resize', ()=>{
            this.setCancansFullScreen()
        });
        element.addEventListener("click",()=>{
            // 获取鼠标点击位置相对于canvas的坐标
            const mouseX = event.clientX - element.offsetLeft;
            const mouseY = event.clientY - element.offsetTop;
            this.scanClick(mouseX,mouseY)
        });
        this.p5init();
        this.test();

    },
    p5init:function (){
        const self = this;
        p5s=new p5((p)=>{
            p.setup=()=>{
                let myCanvas = document.getElementById("wordCanvas");
                offscreenCanvas1 = p.createGraphics(myCanvas.width,myCanvas.height,p.P2D);
                offscreenCanvas2 = p.createGraphics(myCanvas.width,myCanvas.height,p.P2D);
                p.createCanvas(myCanvas.width,myCanvas.height,myCanvas,p.P2D);
                p.frameRate(setting.value.maxFpx)
            }
            p.draw=async ()=>{

                offscreenCanvas1.background(220);
                 self.pushScreen(offscreenCanvas1);
                p.image(offscreenCanvas1,0,0)


            }
        },document.getElementById("wordCanvas"));
        console.log("p5  加载1完成")
    },
    pushScreen:  function(p){

        for (let j = 0; j <= 10; j++) {
            for (let i = 0; i < screen.objList.length; i++) {
                if (screen.objList[i].zIndex==j){

                    screen.objList[i].draw(p);
                }

            }
        }

    },
    scanClick:function (x,y){
        let targets=screen.objList.filter(item=>{
            if (utils.isColliding(item, {
                x: x,
                y: y,
                width: 10,
                height: 10
            })) {
                return true;
            }
            return false;
        })
        for (let i = 0; i < targets.length; i++) {
            targets[i].onClick();
        }
    },
    setCancansFullScreen:function (){

        element.width=window.innerWidth;
        element.height=window.innerHeight;
        screen.width=element.width;
        screen.height=element.height;
        new scanResize(c,screen);
    },
    test:function () {
        // new message("he..l")
    },
    addWord:function (content,des,level){
        // if (content==)
        let res=false;
        screen.objList.filter(item=>{
            if (item.context==content){
                res=true;
            }
        })
        if (res==true){
            ElMessage({
                type:'error',
                message:"重复的单词"+content
            })
            return;
        }
        new word(content,des,c,level);
    },
    removeWord:function (content){
       screen.objList= screen.objList.filter(item=>{
            if (item.context==content)return false;
            return true;
        })
    },
    getAllWords:function (){
        let worlds= screen.objList.map(item=>{

            if (item.context!=undefined){
                return item.context;
            }
            return;

        })
        worlds=worlds.filter(item=>{
            if (item==undefined)return false;
            return true;
        })
        return worlds;
    },
    wordOnClick:function (fun){
        screen.wordOnClick=fun;
    },
    wordOnDoubleClick:function (fun){
        screen.wordDoubleClick=fun;
    },
    chooseWord:function (content){
        screen.objList.map(item=>{
            if (item.context==content){
                item.isSelected=true;
            }
            return item;
        })
    },
    chooseOrCancelWord:function (content){
        screen.objList.map(item=>{
            if (item.context==content){
                item.isSelected=!item.isSelected;
            }
            return item;
        })
    },
    getAllChooseWord:function (){
        let res=screen.objList.map(item=>{
            if (item.isSelected==true)return item.context;
        })
        res=res.filter(item=>{
            if (item != undefined) {
                return true;
            }
        })
        return res;
    },
    clearAllWord:function (){
        screen.objList=screen.objList.filter(item=>{
            if (item.name == "单词") {
                return false;
            }
            return true;
        });

    },
    changeWord:function (content,des,level){
        screen.objList=screen.objList.map(item=>{
            if (item.context==content){
                item.des=des;
                item.level=level;
            }
            return item;
        })

    },
    loadAndPushMyWord(){
     getMyWord({}).then(d=>{
        let words=d.data.data.list;
        this.clearAllWord();
        words.forEach(item=>{
            this.addWord(item.content,item.description,item.level);
        })
    })
    }
}